package com.ctshk.rpc.order.goods.mapper.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.goods.dto.WriteOffRecordPageListDTO;
import com.ctshk.rpc.order.goods.req.OrderPageListPageReq;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;

public class WriteOffRecordProvider {
    public String findWriteOffRecordPageByCondition(IPage<WriteOffRecordPageListDTO> page, OrderPageListPageReq req) {
        String sql = new SQL() {{
            SELECT("t.id,t.write_off_order_number,t.write_off_user_id,t.write_off_user_name," +
                    "t.supplier_id,t.supplier_name,t.write_off_number,t.write_off_details,t.write_off_type," +
                    "t.order_number_id,t.order_number,t.write_off_time,t.gmt_create,t.create_id,t.gmt_modified," +
                    "t.modified_id,t.is_deleted ");
            FROM("`ctshk-goods-order`.t_write_off_record t " +
                    "LEFT JOIN `ctshk-goods-order`.t_order o ON (o.id = t.order_number_id) " +
                    "LEFT JOIN `ctshk-goods-order`.t_order_shipper_info osi ON (osi.order_id = t.order_number_id)");
            WHERE("t.is_deleted = 0");
            // 核销用户id
            if (null != req.getWriteOffUserId()){
                WHERE("t.write_off_user_id = #{req.writeOffUserId}");
            }
            // 供应商名称
            if (StringUtils.isNotBlank(req.getSupplierName())){
                WHERE("t.supplier_name like concat('%',#{req.supplierName},'%')");
            }
            // 核销类型：1-商品核销(该核销方式暂时不做)；2-订单核销
            if (null != req.getWriteOffType() && req.getWriteOffType() > 0) {
                WHERE("t.write_off_type = #{req.writeOffType}");
            }
            // 提货门店id
            if (null != req.getShipperStoreId()) {
                WHERE("osi.shipper_store_id = #{req.shipperStoreId}");
            }
        }}.toString();
        return sql;
    }
}
